#include "LevelSwitcher.h"

LevelSwitcher::LevelSwitcher(void) : level(NULL), player(NULL), theGameMenu(NULL), mouseInfo(NULL){
	currentLevel = 0;

	srand(time(NULL));
	
	init = false;
	startGame = false;
}

LevelSwitcher::~LevelSwitcher(void){
	if(level != NULL){
		delete level;
		level = NULL;
	}

	if(player != NULL){
		delete player;
		player = NULL;
	}
}

void LevelSwitcher::CreateNewPlayer(CWeapon::WEAPON player_weapon_track)
{
	if ( player != NULL )
	{
		delete player;
		player = NULL;
	}

	this->player_weapon_track = player_weapon_track;

	this->player = new Character();
	this->player->InitialiseDeclare( player_weapon_track, mouseInfo );
}

bool LevelSwitcher::SavePlayer(void)
{
	if ( player == NULL ) return false;

	bool temp = player->Save();

	if ( temp )
		player_weapon_track = player->Get_Character_Weapon()->GetWeaponTypeID();

	return temp;
}

bool LevelSwitcher::LoadPlayer(void)
{
	this->player_weapon_track = CWeapon::DUALSWORD;
	this->CreateNewPlayer( player_weapon_track );

	bool temp = player->Load();

	if ( temp )
		player_weapon_track = player->Get_Character_Weapon()->GetWeaponTypeID();

	return temp;
}

bool LevelSwitcher::WeaponLevel(const CWeapon::WEAPON weapon, const int amount)
{
	if ( player == NULL ) return false;

	return this->player->Weapon_Level( weapon, amount );
}

bool LevelSwitcher::WeaponUnlock(const CWeapon::WEAPON weapon, const bool unlock)
{
	if ( player == NULL ) return false;

	return this->player->Weapon_Unlock( weapon, unlock );
}

bool LevelSwitcher::WeaponSelect(const CWeapon::WEAPON weapon)
{
	if ( player == NULL ) return false;

	bool temp = this->player->Weapon_Select( weapon );

	if ( temp )
		this->player_weapon_track = player->Get_Character_Weapon()->GetWeaponTypeID();
	
	return temp;
}

void LevelSwitcher::InitLevelSwitcher(MouseInfo *mouseInfo, CGameMenu* theGameMenu, Sound* theSound){
	this->mouseInfo = mouseInfo;
	this->theGameMenu = theGameMenu;
	this->theSound = theSound;
}

void LevelSwitcher::SwitchLevel(int whichLevel){
	if(this->currentLevel != whichLevel){
		this->currentLevel = whichLevel;
		init = false;
	}
}

void LevelSwitcher::InitLevel(void){
	if(!init){
		switch(currentLevel){
			case 1:
				level = new Level1;
				break;
			case 2:
				level = new Level2;
				break;
			case 3:
				level = new Level3;
				break;
			case 4:
				level = new Level4;
				break;
			default:
				break;
		}

		if(currentLevel > 0)
		{
			if(theGameMenu->MissionButtonType == CGameMenu::EASY_MISSION_ONE)
			{
				level->InitLevel(player, Level::QUESTTYPE::COLLECT, BaseQuest::DIFFICULTY::EASY);
				init = true;
			}
			else if(theGameMenu->MissionButtonType == CGameMenu::EASY_MISSION_TWO)
			{
				level->InitLevel(player, Level::QUESTTYPE::HUNT, BaseQuest::DIFFICULTY::EASY);
				init = true;
			}
			else if(theGameMenu->MissionButtonType == CGameMenu::EASY_MISSION_THREE)
			{
				level->InitLevel(player, Level::QUESTTYPE::PROTECT, BaseQuest::DIFFICULTY::EASY);
				init = true;
			}
			else if(theGameMenu->MissionButtonType == CGameMenu::NORMAL_MISSION_ONE)
			{
				level->InitLevel(player, Level::QUESTTYPE::COLLECT, BaseQuest::DIFFICULTY::NORMAL);
				init = true;
			}
			else if(theGameMenu->MissionButtonType == CGameMenu::NORMAL_MISSION_TWO)
			{
				level->InitLevel(player, Level::QUESTTYPE::HUNT, BaseQuest::DIFFICULTY::NORMAL);
				init = true;
			}
			else if(theGameMenu->MissionButtonType == CGameMenu::NORMAL_MISSION_THREE)
			{
				level->InitLevel(player, Level::QUESTTYPE::PROTECT, BaseQuest::DIFFICULTY::NORMAL);
				init = true;
			}
			else if(theGameMenu->MissionButtonType == CGameMenu::HARD_MISSION_ONE)
			{
				level->InitLevel(player, Level::QUESTTYPE::COLLECT, BaseQuest::DIFFICULTY::HARD);
				init = true;
			}
			else if(theGameMenu->MissionButtonType == CGameMenu::HARD_MISSION_TWO)
			{
				level->InitLevel(player, Level::QUESTTYPE::HUNT, BaseQuest::DIFFICULTY::HARD);
				init = true;
			}
			else if(theGameMenu->MissionButtonType == CGameMenu::HARD_MISSION_THREE)
			{
				level->InitLevel(player, Level::QUESTTYPE::PROTECT, BaseQuest::DIFFICULTY::HARD);
				init = true;
			}
		}
	}
}

void LevelSwitcher::EndLevel(void)
{
	startGame = false;
	init = false;
}

void LevelSwitcher::RenderLevel(void){
	if(startGame){
		//render background
		theGameMenu->DrawEasyMap();

		level->RenderLevel();
		player->RenderObject();
		
		player->GetInventory()->RenderInventory();
	}
}

void LevelSwitcher::UpdateLevel(bool *myKeys, float elapsed){

	if(startGame && init)
	{
		//level->UpdateLevel(elapsed);
		if(myKeys[ 'i' ] == true)
		{
			//Reserved for inventory
			player->GetInventory()->SetDraw(!player->GetInventory()->GetDraw());
			cout << "Inventory " << player->GetInventory()->GetDraw() << endl;
			myKeys[ 'i' ] = false;
		}

		if(myKeys[ 'o' ] == true)
		{
			if(myKeys[ '1' ] == true)
			{
			//Reserved for inventory debugging
			player->GetInventory()->AddItem(Item(Item::no::POTIONS));
			//theInventory->SetInventory(2,2,Item(Item::no::POTIONS,Item::type::CONSUMABLES,""));
			myKeys[ 'o' ] = false;
			myKeys[ '1' ] = false;
			}
			else
			if(myKeys[ '2' ] == true)
			{
			//Reserved for inventory debugging
			player->GetInventory()->AddItem(Item(Item::no::LEATHER));
			//theInventory->SetInventory(2,2,Item(Item::no::POTIONS,Item::type::CONSUMABLES,""));
			myKeys[ 'o' ] = false;
			myKeys[ '2' ] = false;
			}
		}

		if(myKeys[ GLUT_KEY_LEFT ] == true)
		{
			if ( player->Move_Character_Horizontal(true, elapsed) )
			{
				if (theGameMenu->BGM == true)
				{
					theSound->Play(Sound::WALK, false);
				}
			}
		}
		else if(myKeys[ GLUT_KEY_RIGHT ] == true)
		{
			if ( player->Move_Character_Horizontal(false, elapsed) )
			{
				if (theGameMenu->BGM == true)
				{
					theSound->Play(Sound::WALK, false);
				}
			}
		}
		else
		{
			player->Set_Character_Moving( false );		
			theSound->Pause(Sound::WALK);
		}

		if(startGame){

			static bool temp_space_check = false;
			if( !temp_space_check && myKeys[ VK_SPACE ] == true )
			{
				temp_space_check  = true;

				if ( player->GetOnGround() )
				{
					if ( player->Move_Character_Jump() )
					{
						if (theGameMenu->BGM == true)
						{
							theSound->Play(Sound::JUMP);
						}
							AchAddJump(1);
					}
				}
			}
			else temp_space_check = false;

			static bool temp_z_check = false;
			if( !temp_z_check && myKeys[ 'z' ] == true )
			{
				temp_z_check  = true;

				if ( player->Move_Character_Attack( true ) )
				{
					if (theGameMenu->BGM == true)
					{
						theSound->Play(Sound::CHARATTACK);
					}
				}
			}
			else temp_z_check = false;

			if( myKeys[ 'x' ] == true )
			{
				if ( player->Move_Character_Attack( false ) )
				{
					if (theGameMenu->BGM == true)
					{				
						theSound->Play(Sound::CHARATTACK);
					}
				}
			}
			else if ( myKeys[ 'x' ] == false )
			{
				player->Weapon_Sec_Atk_Stop_Hold();
			}

			if( myKeys[ 'c' ] == true )
			{
				if(player->GetInventory()->CheckQuantity(Item::no::POTIONS) > 0 && player->GetLife() < 3.0f){
					player->GetInventory()->RemoveItem(Item::no::POTIONS, 1);
					player->SetLife(player->GetLife() + 0.5f);
				}
			}

			// update level and player
			if ( !player->UpdateObject( elapsed ) || level->UpdateLevel( elapsed ) )
			{
				// save
				this->player->Save();

				// end the level
				this->EndLevel();
			}
		}
	}
	else if ( theGameMenu->MenuType == CGameMenu::MISSION_MENU )
	{
		if(myKeys[ VK_RETURN ])
		{
			startGame = true;
			init = false;

			if(theGameMenu->MissionButtonType >= 0 && theGameMenu->MissionButtonType <= 2)
			{
				currentLevel = 1;
			}
			else if(theGameMenu->MissionButtonType >= 3 && theGameMenu->MissionButtonType <= 5)
			{
				currentLevel = 2;
			}
			else if(theGameMenu->MissionButtonType >= 6 && theGameMenu->MissionButtonType <= 8)
			{
				currentLevel = 3;
			}

			player->Load();
			player->GetInventory()->SetDraw( false );

			this->player_weapon_track = player->Get_Character_Weapon()->GetWeaponTypeID();
		}
	}
}

Vector3D LevelSwitcher::ReturnCharPos (void)
{
	if(init && startGame)
		return player->GetPosition();
	else
		return 0;
}